package exttable.view;

import javax.swing.*;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jul 9, 2010
 * Time: 5:20:52 PM
 * To change this template use File | Settings | File Templates.
 */
public class ViewInputFormComponents {
    private JLabel label;
    private JComponent component;
    private JCheckBox checkBox;
    private ViewAnalysis analysis;
    private boolean emptyValue;

    public JLabel getLabel() {
        return label;
    }

    public void setLabel(JLabel label) {
        this.label = label;
    }

    public JComponent getComponent() {
        return component;
    }

    public void setComponent(JComponent component) {
        this.component = component;
    }

    public JCheckBox getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(JCheckBox checkBox) {
        this.checkBox = checkBox;
    }

    public ViewAnalysis getAnalysis() {
        return analysis;
    }

    public void setAnalysis(ViewAnalysis analysis) {
        this.analysis = analysis;
    }

    public boolean isEmptyValue() {
        return emptyValue;
    }

    private void setEmptyValue(boolean emptyValue) {
        this.emptyValue = emptyValue;
    }

    public void setComponentEnable(boolean a) {
        label.setEnabled(a);
        component.setEnabled(a);
        if (component instanceof JPanel) {
            JRadioButton jb1 = (JRadioButton) component.getComponent(0);
            JRadioButton jb2 = (JRadioButton) component.getComponent(1);
            jb1.setEnabled(a);
            jb2.setEnabled(a);
        }
    }

    public void componentValidator() {

        if (component instanceof JTextField) {
            setEmptyValue(filedValidator((JTextField) component));
        }
        if (component.getClass().getSimpleName().equals("JPanel")) {
            setEmptyValue(filedValidator((JPanel) component));
        }
        if (component instanceof JTextArea) {
            setEmptyValue(filedValidator((JTextArea) component));
        }
    }

    private boolean filedValidator(JTextField tf) {
        return tf.getText().length() == 0;
    }

    private boolean filedValidator(JPanel panel) {
        JRadioButton jb1 = (JRadioButton) panel.getComponent(0);
        JRadioButton jb2 = (JRadioButton) panel.getComponent(1);
        return !jb1.isSelected() & !jb2.isSelected();
    }

    private boolean filedValidator(JTextArea jta) {
        return jta.getText().length() == 0;
    }

}
